package com.javabasic.algorithm.sword_of_offer;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/6 10:54 下午
 */
public class 剑指_Offer_06_从尾到头打印链表 {

    /**
     * 思维逻辑，链表反转
     * @param head
     * @return
     */
    public int[] reversePrint(ListNode head) {
        int len = 0;

        /**
         * 计算链表长度
         */
        ListNode fakeHead = new ListNode(-1), temp;
        fakeHead.next = head;
        temp = fakeHead;
        while (temp.next != null) {
            temp = temp.next;
            len++;
        }

        // 初始化数组
        int[] result = new int[len];

        /**
         * 链表转数组
         */
        temp = fakeHead;
        int index = 0, tag;
        while (temp.next != null) {
            temp = temp.next;
            result[index++] = temp.val;
        }

        /**
         * 数组反转
         */
        for (int i = 0; i < len / 2; i++) {
            tag = result[i];
            result[i] = result[len-i-1];
            result[len-i-1] = tag;
        }
        return result;
    }


    /**
     * 直接反转链表
     * @param head
     * @return
     */
    public int[] reversePrint2(ListNode head) {

        return null;
    }
}
